package stream

import (
	"fmt"

	rc "iot-base/common/redis"

	"github.com/vmihailenco/msgpack/v5"
)

//VideoInfo  视频信息
type VideoAddrOption struct {
	RTSP      string `json:"rtsp_over_rtsp"`
	RTMP      string `json:"rtmp"`
	HLS       string `json:"hls"`
	FlvOverWS string `json:"flv_over_ws"`
	//FlvOverHttp string `json:"flv_over_http"`
}

//EncodingOption 编码信息
type EncodingOption struct {
	EncodingType string `json:"encoding_type"`
	VWidth       int    `json:"v_wdith"`
	VHeight      int    `json:"v_height"`
	Profile      string `json:"profile"`
	BitRateLimit int    `json:"bit_rate_limit"`
	FrameRate    int    `json:"frame_rate"`
	GOP          int    `json:"gop"`
	Quality      int    `json:"quality"`
}

type StreamOption struct {
	TID   string `json:"tid"`
	SubID string `json:"subid"`
	PID   string `json:"pid"`
	//Index           int              `json:"index"`
	Name             string `json:"name"`
	VideoTokenAddr   string `json:"raw_addr_token"`
	RawAddr          string `json:"raw_addr"`
	Token            string `json:"token"`
	IsRotate         bool   `json:"is_rotate"`
	VideoSourceToken string `json:"video_source_token"`
	//VideoAddrOption *VideoAddrOption `json:"video_addr_option"`
	EncodingOption  EncodingOption  `json:"encoding_option"`
	VideoAddrOption VideoAddrOption `json:"video_addr_option"`
}
type StreamAddr struct {
	StreamOptions []StreamOption `json:"stream_options"`
	DispatchAddr  string         `json:"dispatch_addr"`
	DispatchID    string         `json:"dispatch_id"`
	State         string         `json:"state"`
}

func makeKeyID(tid string) string {
	return "sdata." + tid + ".#stream_option#."
}

func makeFieldID(subid string, index string) string {
	return fmt.Sprintf("%s.%s", subid, index)
}

//Remove
func RemoveStreamOption(tid string) error {
	keyID := makeKeyID(tid)
	_, err := rc.Del(keyID).Result()
	return err
}

//SaveStreamOption 存储视频信息
func SaveStreamOption(option *StreamOption) error {
	keyID := makeKeyID(option.TID)
	fieldID := makeFieldID(option.SubID, option.Token)
	bytes, err := msgpack.Marshal(option)
	//fieldAddrToken := fieldID + ".addr"

	if err == nil {
		_, err = rc.HSet(keyID, fieldID, bytes).Result()
		return err

	}
	return err
}

//SetDispatch
func SetDispatch(tid string, did string, daddr string) {
	id := makeKeyID(tid)

	rc.HSet(id, "DispatchID", did)
	rc.HSet(id, "DispatchAddr", daddr)
}

//GetStreamOptions 获取视频信息
func GetStreamOptions(tid string) *StreamAddr {
	keyID := makeKeyID(tid)
	optionMap, err := rc.HGetAll(keyID).Result()
	if err != nil {
		return nil
	}
	if len(optionMap) == 0 {
		return nil
	}
	res := make([]StreamOption, 0, len(optionMap))
	streamAddr := &StreamAddr{
		StreamOptions: res,
	}
	count := 0
	//left := make([]string,0,3)
	for k, v := range optionMap {
		if k == "DispatchAddr" {
			streamAddr.DispatchAddr = v
		} else if k == "DispatchID" {
			streamAddr.DispatchID = v
		} else {
			var option StreamOption
			msgpack.Unmarshal([]byte(v), &option)
			res = append(res, option)
			streamAddr.StreamOptions = res
			count++
		}
	}

	return streamAddr
}
